// pages/index/index.js
const app = getApp();
// let login = require('../../../config/login');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    homeData: null,
    domain: app.api.domain,
    isTop: true,
    shade: false,
    currentIndex: 0,
    toggle: true,
    partData: {
      imgUrls: '',
      name: '',
      inventory: 0,
      price: '',
      goods_id: '',
      detail:false
    },
  },

  // 跳转到商品详情
  skipDetails(e) {
    let goodsId = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: `/page/detail/pages/details/details?id=${goodsId}`
    })
  },
  // 跳转到搜索页
  toSearch() {
    wx.navigateTo({
      url: '/page/detail/pages/search/search',
    })
  },
  // 轮播图跳转
  toSwiperDetail(e) {
    const that = this;
    let type = e.currentTarget.dataset.type;
    let redirect_type = e.currentTarget.dataset.redirect_type;
    let content = e.currentTarget.dataset.content;
    let aid = e.currentTarget.dataset.aid;
    let share_img = e.currentTarget.dataset.shareimg;
    if (type == undefined) {
      type = ''
    }
    switch (redirect_type) {
      case 0: // 不跳转
        break;
      case 1: // 跳转详情页
        wx.navigateTo({
          url: `/page/detail/pages/details/details?id=${content}&type=${type}`
        })
        break;
      case 2: // 跳转分类页
        app.globalData.classifyIndex = (content !== '' ? parseInt(content) : 0);
        app.globalData.pos_type = type;
        wx.switchTab({
          url: '/page/tabBar/classify/classify'
        })
        break;
      case 3: // 跳转webview
        console.log(content)
        wx.navigateTo({
          url: `/page/detail/pages/webview/webview?url=${content}`
        })
        break;
      case 4: // 跳转秒杀页
        wx.navigateTo({
          url: `/page/detail/pages/seckill/seckill?type=${type}&share_img=${share_img}`
        })
        break;
      case 5: // 跳转优惠券页
        app.checkUserInfoAuthorizationCart().then(() => {
          wx.navigateTo({
            url: `/page/detail/pages/tickets/tickets?type=${type}&share_img=${share_img}`
          })
        })
        break;
      case 6: // 跳转活动页
        wx.navigateTo({
          url: `/page/detail/pages/activityPage/activityPage?goods_ids=${aid}&type=${type}&share_img=${share_img}`
        })
        break;
      case 7: // 跳转活动页
        wx.navigateTo({
          url: `/page/details/pages/cardHolder/cardHolder`
        })
        break;
    }
  },
  // 添加到购物车
  // addToCart(e) {
  //   const that = this;
  //   let valid = e.currentTarget.dataset.valid;
  //   if (valid == 0) {
  //     app.showInfo('此商品不存在或者已下架');
  //     return
  //   }
  //   app.checkUserInfoAuthorizationCart().then(() => {
  //     let goodsid = e.currentTarget.dataset.goodsid;
  //     let token = app.globalData.token;
  //     let json = {
  //       token,
  //       goods_num: 1,
  //       goods_id: goodsid,
  //     }
  //     let jsonData = app.utils.getSignData(json);
  //     that.setData({
  //       hiddenLoading: false
  //     })
  //     app.utils.post(app.api.reqUrls.addCart, jsonData).then(res => {
  //       that.setData({
  //         hiddenLoading: true
  //       })
  //       if (res.status == 1) {
  //         wx.showToast({
  //           title: '已添加到购物车',
  //           icon: 'success',
  //           duration: 1000
  //         })
  //         app.getCartCount();
  //       } else {
  //         throw res.msg;
  //       }
  //     }).catch(err => {
  //       app.showInfo(err.toString())
  //       that.setData({
  //         hiddenLoading: true
  //       })
  //     })
  //   })

  // },
  // 获取首页数据
  getAll() {
    const that = this;
    let json = {
      token: app.globalData.token,
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getHomePageData, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        app.getImg(res.result.high_quality_goods);

        if (res.result.circle_list && res.result.circle_list.length > 10) {
          res.result.circle_list = app.utils.arrTrans(2, res.result.circle_list);
        }
        that.setData({
          homeData: res.result
        })
        setTimeout(() => {
          wx.stopPullDownRefresh()
        }, 1000)
      } else {
        wx.showModal({
          content: res.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(res) {
            if (res.confirm) {
              that.location()
            }
          }
        })
      }
    }).catch(err => {
      app.showInfo(err.toString())
      that.setData({
        hiddenLoading: true
      })
      setTimeout(() => {
        wx.stopPullDownRefresh()
      }, 1000)
    })
  },

  // 点击添加购物车
  addShowCart(e) {
    const that = this;
    let goods = e.currentTarget.dataset.goods
    that.setData({
      toggle: false,
      'partData.imgUrls': goods.img,
      'partData.name': goods.goods_name,
      'partData.inventory': goods.store_count,
      'partData.price': goods.shop_price,
      'partData.goods_id': goods.goods_id,
    })
  },
  // 添加到购物车
  addToCart(e) {
   
    app.checkUserInfoAuthorizationCart().then(() => {
      const that = this;
      let token = app.globalData.token;
      let json = {
        token,
        goods_num: e.detail,
        goods_id: that.data.partData.goods_id,
      }
      let jsonData = app.utils.getSignData(json);
      app.utils.post(app.api.reqUrls.addCart, jsonData).then(res => {
        if (res.status == 1) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000
          })
          app.getCartCount();
          app.cartCountCallback = (res) => {
            that.setData({
              cartCount: res
            })
          }
        } else {
          throw res.msg
        }
      }).catch(err => {
        app.showInfo(err.toString())
      })

    })
  },
  onLoad: function (options) {
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    const _that = this;
    _that.setData({
      shade: app.globalData.shade,
      toggle: true,
    })
    const token = wx.getStorageSync('token');
    if (token) {
      _that.getAll();
    } else {
      app.appCallBack = (flag) => {
        //如需要加载完app.js再执行的可通过此方式执行
        _that.getAll();
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    const that = this;
    that.setData({
      toggle: true,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      currentIndex: 0
    })
  },

  onPageScroll(res) {
    if (res.scrollTop > 0) {
      this.setData({
        isTop: false
      })
    } else {
      this.setData({
        isTop: true
      })
    }
  },
  //分享（不带任何参数）
  onShareAppMessage: function () {
    // const that = this;
    // const nowStoreId = that.data.homeData.select_store_info.store_id;
    // return {
    //   title: that.data.homeData.select_store_info.store_name,
    //   path: `/page/tabBar/index/index?index_store_id=${nowStoreId}`, // 分享后打开的页面
    //   imageUrl: that.data.domain + that.data.homeData.select_store_info.store_logo
    // }
  },
})